package com.groupbuying.storeinternal.service.impl;

import com.groupbuying.storeinternal.constants.CommonConstants;
import com.groupbuying.storeinternal.constants.StoreOpexRuleRelPreSearchConstants;
import com.groupbuying.storeinternal.constants.StoreOpexRuleRelSearchConstants;
import com.groupbuying.storeinternal.dao.StoreOpexRuleRelDao;
import com.groupbuying.storeinternal.domain.StoreMainInfoDO;
import com.groupbuying.storeinternal.domain.StoreOpexRuleRelDO;
import com.groupbuying.storeinternal.domain.StoreOpexRuleRelPreDO;
import com.groupbuying.storeinternal.service.StoreMainInfoService;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelPreService;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelService;
import com.groupbuying.storeinternal.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class StoreOpexRuleRelServiceImpl implements StoreOpexRuleRelService {
    private static Logger log = LoggerFactory.getLogger(StoreOpexRuleRelServiceImpl.class);

    @Autowired
    private StoreOpexRuleRelDao storeOpexRuleRelDao;

    @Autowired
    private StoreOpexRuleRelPreService storeOpexRuleRelPreService;

    @Autowired
    private StoreMainInfoService storeMainInfoService;

    @Override
    public StoreOpexRuleRelDO get(Integer relId) {
        return storeOpexRuleRelDao.get(relId);
    }

    @Override
    public List<StoreOpexRuleRelDO> list(Map<String, Object> map) {
        return storeOpexRuleRelDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return storeOpexRuleRelDao.count(map);
    }

    @Override
    public int save(StoreOpexRuleRelDO storeOpexRuleRel) {
        return storeOpexRuleRelDao.save(storeOpexRuleRel);
    }

    @Override
    public int update(StoreOpexRuleRelDO storeOpexRuleRel) {
        return storeOpexRuleRelDao.update(storeOpexRuleRel);
    }

    @Override
    public int remove(Integer relId) {
        return storeOpexRuleRelDao.remove(relId);
    }

    @Override
    public int batchRemove(Integer[] relIds) {
        return storeOpexRuleRelDao.batchRemove(relIds);
    }

    @Override
    public R pushPreOpexToMaster(String storeUuid){
        //1.先搜索预表
        Calendar now = Calendar.getInstance();
        boolean isFirstSunday = (now.getFirstDayOfWeek() == Calendar.SUNDAY);
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) {
                weekDay = 7;
            }
        }
        log.info("更新时间，星期" + weekDay);

        Map searchMapPre = new HashMap();
        searchMapPre.put(StoreOpexRuleRelPreSearchConstants.SEARCH_STOREID,storeUuid);
        searchMapPre.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DELFLAG,CommonConstants.COMMON_FLAG_UNDEL);
        searchMapPre.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DAYID, weekDay);
        List<StoreOpexRuleRelPreDO> listRuleRelPre = storeOpexRuleRelPreService.list(searchMapPre);

        if(listRuleRelPre == null || listRuleRelPre.size() == 0){
            //未设置规则
            //搜索规则并删除
            Map searchMap = new HashMap();
            searchMap.put(StoreOpexRuleRelSearchConstants.SEARCH_STOREID,storeUuid);
            searchMap.put(StoreOpexRuleRelSearchConstants.SEARCH_DELFLAG,CommonConstants.COMMON_FLAG_UNDEL);
            List<StoreOpexRuleRelDO> listRuleRel = this.list(searchMap);
            if(listRuleRel == null || listRuleRel.size() == 0){
                return R.ok();
            }
            StoreOpexRuleRelDO storeOpexRuleRelDO = listRuleRel.get(0);
            storeOpexRuleRelDO.setDelFlag(CommonConstants.COMMON_FLAG_DEL);
            storeOpexRuleRelDO.setGmtModified(new Date());
            this.update(storeOpexRuleRelDO);
            return R.ok();
        }

        StoreOpexRuleRelPreDO storeOpexRuleRelPreDO = listRuleRelPre.get(0);
        //搜索本店正式规则
        Map searchMap = new HashMap();
        searchMap.put(StoreOpexRuleRelSearchConstants.SEARCH_STOREID,storeUuid);
        List<StoreOpexRuleRelDO> listRuleRel = this.list(searchMap);
        if(listRuleRel == null || listRuleRel.size() == 0){
            //新增逻辑
            StoreOpexRuleRelDO storeOpexRuleRelDO = new StoreOpexRuleRelDO();
            storeOpexRuleRelDO.setGmtModified(new Date());
            storeOpexRuleRelDO.setDelFlag(CommonConstants.COMMON_FLAG_UNDEL);
            storeOpexRuleRelDO.setDayId(storeOpexRuleRelPreDO.getDayId());
            storeOpexRuleRelDO.setStoreId(storeOpexRuleRelPreDO.getStoreId());
            storeOpexRuleRelDO.setRuleId(storeOpexRuleRelPreDO.getRuleId());
            storeOpexRuleRelDO.setRuleDes(storeOpexRuleRelPreDO.getRuleDes());
            this.save(storeOpexRuleRelDO);
        } else {
            //更新逻辑
            StoreOpexRuleRelDO storeOpexRuleRelDO = listRuleRel.get(0);
            storeOpexRuleRelDO.setRuleId(storeOpexRuleRelPreDO.getRuleId());
            storeOpexRuleRelDO.setGmtModified(new Date());
            storeOpexRuleRelDO.setDelFlag(CommonConstants.COMMON_FLAG_UNDEL);
            storeOpexRuleRelDO.setDayId(storeOpexRuleRelPreDO.getDayId());
            storeOpexRuleRelDO.setRuleDes(storeOpexRuleRelPreDO.getRuleDes());
            this.update(storeOpexRuleRelDO);
        }
        return  R.ok();
    }

    @Override
    public R batchPushPreOpexToMaster(){
        //搜索全部门店
        Map searchStoreMap = new HashMap();
        searchStoreMap.put("delflag",CommonConstants.COMMON_FLAG_UNDEL);
        List<StoreMainInfoDO> listStore = storeMainInfoService.list(searchStoreMap);
        for(StoreMainInfoDO storeMainInfoDO : listStore){
            log.info("开始更新门店《" + storeMainInfoDO.getStorename() + "》优惠规则");
            this.pushPreOpexToMaster(storeMainInfoDO.getUuid());
            log.info("完成更新门店《" + storeMainInfoDO.getStorename() + "》优惠规则");
        }
        return R.ok();
    }

    /**
     * Description 根据storeID获取门店运营规则 <br>
     *
     * @param storeID
     * @return opexRuleID
     * @author Ywxd <br>
     * @createDate 2019/4/18 17:11<br>
     **/
    @Override
    public String getOpexRuleByStoreID(String storeID) {
        return storeOpexRuleRelDao.getOpexRuleByStoreID(storeID);
    }

}
